//
//  ModifyPhotoView.m
//  HpnotiqCustomize
//
//  Created by Harmandeep Singh on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ModifyPhotoView.h"
#import <QuartzCore/QuartzCore.h>



@interface ModifyPhotoView()

- (void)configureToShowSelectionOnItemView:(ModifyItemView *)view;
- (void)configureToHideSelectionOnItemView:(ModifyItemView *)view;

@end






@implementation ModifyPhotoView

@synthesize mSelectedItemView;
@synthesize viewDelegate;



- (id)initWithImage:(UIImage *)image withinRect:(CGRect)withinFrame
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        // Initialization code
        
        

        // Disable Multitouch on this PhotoView
        self.multipleTouchEnabled = NO;
        
        
        
        
        // PhotoView Positioning in its superView
        CGFloat widthForPhotoImageView = 0;
        CGFloat heightForPhotoImageView = 0;
        
        CGFloat imageRatio = image.size.width / image.size.height;
        
        if(imageRatio >= 1)
        {
            // Landscape Photo
            
            widthForPhotoImageView = withinFrame.size.width;
            heightForPhotoImageView = widthForPhotoImageView / imageRatio;
        }
        else
        {
            // Portrait Photo
            
            heightForPhotoImageView = withinFrame.size.height;
            widthForPhotoImageView = imageRatio * heightForPhotoImageView;
        }
        
        
        UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                                    0,
                                                                                    roundf(widthForPhotoImageView),
                                                                                    roundf(heightForPhotoImageView))];
        photoImageView.image = image;
        [self addSubview:photoImageView];
        [photoImageView release];
        
        
        
        self.frame = CGRectMake(roundf((withinFrame.size.width - photoImageView.frame.size.width) / 2),
                                roundf((withinFrame.size.height - photoImageView.frame.size.height) / 2),
                                photoImageView.frame.size.width,
                                photoImageView.frame.size.height);
    }
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)dealloc {
    
    [mSelectedItemView release];
    [super dealloc];
}




- (void)addModifyItemView:(ModifyItemView *)view withOrigin:(CGPoint)viewOrigin
{
    view.frame = CGRectMake(viewOrigin.x,
                            viewOrigin.y,
                            view.frame.size.width,
                            view.frame.size.height);
    [self addSubview:view];
    
    
    
    
    if(mSelectedItemView)
    {
        [self configureToHideSelectionOnItemView:mSelectedItemView];
    }
    
    [self configureToShowSelectionOnItemView:view];
    self.mSelectedItemView = view;
}





- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [touches anyObject];
    UIView *touchedView = [touch view];
    
    
    if([touchedView isKindOfClass:[ModifyItemView class]])
    {
        CGPoint touchPoint = [touch locationInView:self];
        
        if(![(ModifyItemView *)touchedView mIsBeingMoved])
        {
            NSLog(@"NO");
            
            [(ModifyItemView *)touchedView setMIsBeingMoved:YES];
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options:UIViewAnimationCurveLinear
                             animations:^{
                                 
                                 touchedView.center = touchPoint;
                             } 
                             completion:^(BOOL finished) {
                                 
                                 
                             }];
        }
        
        touchedView.center = touchPoint;
        	
    }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [touches anyObject];
    UIView *touchedView = [touch view];
    
    
    if(touchedView != mSelectedItemView)
    {
        if([touchedView isKindOfClass:[ModifyItemView class]])
        {
            [(ModifyItemView *)touchedView setMIsBeingMoved:NO];
            
            if(mSelectedItemView)
            {
                [self configureToHideSelectionOnItemView:mSelectedItemView];
            }
            
            [self configureToShowSelectionOnItemView:(ModifyItemView *)touchedView];
            
            self.mSelectedItemView = (ModifyItemView *)touchedView;
        }
        else
        {
            [self configureToHideSelectionOnItemView:mSelectedItemView];
            self.mSelectedItemView = nil;
        }
        
        
        if([viewDelegate respondsToSelector:@selector(modifyPhotoView:newlySelectedObject:)])
        {
            [viewDelegate modifyPhotoView:self newlySelectedObject:mSelectedItemView];
        }
    }

}


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	
//	/*
//     To impose as little impact on the device as possible, simply set the placard view's center and transformation to the original values.
//     */
//	placardView.center = self.center;
//	placardView.transform = CGAffineTransformIdentity;
}





- (void)configureToShowSelectionOnItemView:(ModifyItemView *)view
{
    view.layer.borderWidth = 2.0;
}


- (void)configureToHideSelectionOnItemView:(ModifyItemView *)view
{
    view.layer.borderWidth = 0.0;
}


@end
